<!-- Copyright (c) 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->

<link rel="import" href="../common/cr-action.html">
<link rel="import" href="../common/cr-butterbar.html">
<link rel="import" href="../common/cr-button.html">
<link rel="import" href="../common/cr-keyboard.html">
<link rel="import" href="../common/cr-toolbar.html">
<link rel="import" href="../common/cr-view-handler.html">
<link rel="import" href="../diff/cr-diff-cursor.html">
<link rel="import" href="../linkify/cr-linkified-text.html">
<link rel="import" href="cr-issue-messages.html">
<link rel="import" href="cr-issue-meta.html">
<link rel="import" href="cr-issue-patchsets.html">
<link rel="import" href="cr-issue-subject.html">

<polymer-element name="cr-issue" attributes="issue" on-butterbar-update="{{ updateButterbar }}">
    <template>
        <cr-view-handler></cr-view-handler>
        <link rel="stylesheet" href="../common/common.css">
        <link rel="stylesheet" href="cr-issue.css">

        <cr-butterbar message="{{ butterbarMessage }}"></cr-butterbar>

        <cr-issue-subject issue="{{ issue }}"></cr-issue-subject>

        <div class="issue-body">
            <cr-issue-meta issue="{{ issue }}"></cr-issue-meta>

            <div class="issue-content">
                <div class="issue-description">
                    <cr-linkified-text text="{{ issue.description }}" pre></cr-linkified-text>
                </div>
            </div>
        </div>

        <header>
            <h2>Patches</h2>
        </header>

        <cr-issue-patchsets id="patchsets" patchsets="{{ issue.patchsets }}"
                            on-expanded="{{ handlePatchsetToggled }}"
                            on-collapsed="{{ handlePatchsetToggled }}"
                            on-diff-shown="{{ handleDiffViewAltered }}"
                            on-diff-hidden="{{ handleDiffViewAltered }}"
                            on-context-shown="{{ handleDiffViewAltered }}"
                            ></cr-issue-patchsets>

        <template if="{{ issue.messages.length }}">
            <header>
                <h2>Messages</h2>
                <div class="ui-actions">
                    <a is="cr-action" on-tap="{{ expandAllMessages }}">Expand all</a> /
                    <a is="cr-action" on-tap="{{ collapseAllMessages }}">Collapse all</a>
                </div>
            </header>
        </template>

        <cr-issue-messages messages="{{ issue.messages }}" id="messages"></cr-issue-messages>

        <div id="dialogs"></div>

        <cr-diff-cursor id="diffCursor"></cr-cursor-manager>
        <cr-keyboard on-key-j="{{ handleNextPatchKey }}"
                     on-key-k="{{ handlePreviousPatchKey }}"
                     on-key-o="{{ handleOpenTargetPatchKey }}"
                     on-key-right="{{ handleShowTargetPatchKey }}"
                     on-key-left="{{ handleHideTargetPatchKey }}"
                     global></cr-keyboard>
    </template>
    <script>
        Polymer({
            created: function() {
                this.issue = null;
                this.butterbarMessage = "";
            },
            updateButterbar: function(event) {
                this.butterbarMessage = event.detail.message;
            },
            collapseAllMessages: function() {
                this.issue.messages.forEach(function(message) {
                    message.active = false;
                });
            },
            expandAllMessages: function() {
                this.issue.messages.forEach(function(message) {
                    message.active = true;
                });
                this.async(function() {
                    this.$.messages.scrollIntoViewIfNeeded();
                }, null, 350);
            },
            handlePatchsetToggled: function(event) {
                this.$.diffCursor.resetStops();
            },
            handleNextPatchKey: function(event) {
                this.$.diffCursor.next(event, "cr-issue-patchfile");
            },
            handlePreviousPatchKey: function(event) {
                this.$.diffCursor.previous(event, "cr-issue-patchfile");
            },
            handleOpenTargetPatchKey: function(event) {
                var target = this.$.diffCursor.target;
                if (target && target.file)
                    this.$.diffCursor.open(event, target.file.getSingleViewUrl());
            },
            handleShowTargetPatchKey: function(event) {
                if (this.$.diffCursor.target && this.$.diffCursor.target.showDiff) {
                    this.$.diffCursor.target.showDiff();
                } else {
                    this.$.diffCursor.handleMoveToRightColumn(event);
                }
            },
            handleHideTargetPatchKey: function(event) {
                if (this.$.diffCursor.target && this.$.diffCursor.target.hideDiff) {
                    this.$.diffCursor.target.hideDiff();
                } else {
                    this.$.diffCursor.handleMoveToLeftColumn(event);
                }
            },
            handleDiffViewAltered: function(event) {
                this.$.diffCursor.resetStops();
            },
            hasBeenSent: function(issue) {
                return issue.hasBeenSent();
            },
        });
    </script>
</polymer-element>
